# frontend/utils/session_manager.py
import uuid
import streamlit as st
from datetime import datetime
from typing import Dict, List

class SessionManager:
    def __init__(self):
        self.current_session_id = None
        self.sessions = {}
    
    def create_session(self, user_id: str = "default_user") -> str:
        """创建新会话"""
        session_id = str(uuid.uuid4())
        self.current_session_id = session_id
        
        self.sessions[session_id] = {
            'session_id': session_id,
            'user_id': user_id,
            'created_at': datetime.now(),
            'last_activity': datetime.now(),
            'message_count': 0,
            'active_models': []
        }
        
        return session_id
    
    def get_current_session(self) -> str:
        """获取当前会话ID"""
        if not self.current_session_id:
            self.current_session_id = self.create_session()
        return self.current_session_id
    
    def update_session_activity(self, session_id: str):
        """更新会话活动时间"""
        if session_id in self.sessions:
            self.sessions[session_id]['last_activity'] = datetime.now()
            self.sessions[session_id]['message_count'] += 1
    
    def get_session_info(self, session_id: str) -> Dict:
        """获取会话信息"""
        return self.sessions.get(session_id, {})
    
    def list_sessions(self) -> List[Dict]:
        """列出所有会话"""
        return list(self.sessions.values())
